package polly;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import polly.props.SchoolPropertiesConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
@ComponentScan("polly")
@EnableConfigurationProperties(SchoolPropertiesConfiguration.class)
@ConditionalOnProperty(prefix = "school",name = "enabled",havingValue = "true")
@RequiredArgsConstructor
public class SchoolAutoConfiguration {
    @Autowired
    private SchoolPropertiesConfiguration schoolPropertiesConfiguration;

    @Bean
    public School school() {
        List<Integer> studentIds = schoolPropertiesConfiguration.getStudentIds();
        List<String> studentNames = schoolPropertiesConfiguration.getStudentNames();
        List<String> classNames = schoolPropertiesConfiguration.getClassNames();

        List<Student> students = new ArrayList<>();
        for (int i=0; i<studentIds.size(); i++) {
            students.add(Student.builder().id(studentIds.get(i)).name(studentNames.get(i)).build());
        }

        List<Klass> classes = new ArrayList<>();
        for (int i=0; i<classNames.size(); i++) {
            classes.add(Klass.builder().className(classNames.get(i)).build());
        }


        return School.builder().classList(classes).studentList(students).build();
    }
}
